package com.how2java.tmall.web;

import com.how2java.tmall.pojo.Favorite;
import com.how2java.tmall.pojo.User;
import com.how2java.tmall.service.FavoriteService;
import com.how2java.tmall.service.UserService;
import com.how2java.tmall.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class FavoriteController {
    @Autowired
    FavoriteService favoriteService;

    @GetMapping("/favorite")
    public String add(HttpSession session, Integer pid) throws Exception {
        User user = (User) session.getAttribute("user");
        int userId = 0;
        if (user == null) {
            return "/fore/login";
        }
        userId = user.getId();
        System.out.println(userId + "userId为======");
        System.out.println(pid);
        //查fav是否已经存在
        List<Favorite> favorites = favoriteService.findByGoodsIdAndUserId(pid, userId);
        if (favorites.size() > 0) {
            //已经添加过了

        }

        Favorite favorite = new Favorite();
        favorite.setUserId(userId);
        favorite.setGoodsId(pid);

        favoriteService.add(favorite);
        return "fore/product";
    }

}

